package com.test.demo.mydubbo.framework.protocol.dubbo;

import com.test.demo.mydubbo.framework.protocol.DataFormater;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelMetadata;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.DefaultAttributeMap;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

public class NettyClientHandler extends SimpleChannelInboundHandler<String> implements Callable<String> {
    private ChannelHandlerContext context;
    private String result;
    private DataFormater dataFormater;
    private CountDownLatch countDownLatch=new CountDownLatch(1);
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        context=ctx;
    }

    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        result = s;
        countDownLatch.countDown();
    }

    public String call() throws Exception {
        context.channel().writeAndFlush(dataFormater);
        countDownLatch.await();
        return result;
    }

    public void setDataFormater(DataFormater formater){
        dataFormater=formater;
    }
}
